package com.iman.common.util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class RandString {
	/* 字母表类型 */
	public static final int TABLE_DECIMAL				= 1;
	public static final int TABLE_ALPHA					= 2;
	public static final int TABLE_LOWER_ALPHA			= 3;
	public static final int TABLE_UPPER_ALPHA			= 4;
	public static final int TABLE_DECIMAL_ALPHA			= 5;
	public static final int TABLE_DECIMAL_LOWER_ALPHA	= 6;
	public static final int TABLE_HEXADECIMAL			= 7;

	/* 字母表 */
	private static char[] tableDecimal = {
		'0', '1', '2', '3', '4', '5', '6', '7','8', '9'};

	private static char[] tableHexaDecimal = {
		'0', '1', '2', '3', '4', '5', '6', '7','8', '9',
		'a', 'b', 'c', 'd', 'e', 'f'};

	private static char[] tableAlpha = {
		'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
		'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
		'u', 'v', 'w', 'x', 'y', 'z',
		'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
		'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
		'U', 'V', 'W', 'X', 'Y', 'Z',};

	private static char[] tableLowerAlpha = {
		'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
		'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
		'u', 'v', 'w', 'x', 'y', 'z'};

	private static char[] tableUpperAlpha = {
		'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
		'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
		'U', 'V', 'W', 'X', 'Y', 'Z',};

	private static char[] tableDecimalAlpha = {
		'0', '1', '2', '3', '4', '5', '6', '7','8', '9',
		'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
		'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
		'u', 'v', 'w', 'x', 'y', 'z',
		'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
		'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
		'U', 'V', 'W', 'X', 'Y', 'Z',};

	private static char[]  tableDecimalLowerAlpha = {
		'0', '1', '2', '3', '4', '5', '6', '7','8', '9',
		'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
		'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
		'u', 'v', 'w', 'x', 'y', 'z'};

	/* 生成伪UUID串*/
	public static String virtualUUID(String str) {
		if (str == null || str.equals(""))
			return null;

		try {
			MessageDigest md5 = MessageDigest.getInstance("MD5");
			byte[] hash = md5.digest(str.getBytes());

			int a;
			String tmp, uuid = "";
			for (int i=0; i < hash.length; i ++ ) {
				a = 0xff & hash[i];
				tmp = Integer.toHexString(a);
				uuid += tmp.length() == 1 ? "0" + tmp : tmp;

				switch (i) {
				case 3:
				case 5:
				case 7:
				case 9:
					uuid += "-";
					break;
				}
			}

			return uuid;
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}

	/* 生成随机密码  */
	public static String createPassWord(String str) {
		return createPassWord(str, 6);
	}

	public static String createPassWord(String str, int size) {
		return createPassWord(str, size, TABLE_DECIMAL_LOWER_ALPHA);
	}

	public static String createPassWord(String str, int size, int type) {
		String pwd = "";
		char[] table = tableDecimalLowerAlpha;

		if (str == null || str.equals(""))
			return null;
		
		if (size < 6 || size > 16)
			return null;

		//获取字符表和表长度
		switch(type) {
		case TABLE_DECIMAL:
			table = tableDecimal;
			break;
		case TABLE_HEXADECIMAL:
			table = tableHexaDecimal;
			break;
		case TABLE_ALPHA:
			table = tableAlpha;
			break;
		case TABLE_LOWER_ALPHA:
			table = tableLowerAlpha;
			break;
		case TABLE_UPPER_ALPHA:
			table = tableUpperAlpha;
			break;
		case TABLE_DECIMAL_ALPHA:
			table = tableDecimalAlpha;
			break;
		case TABLE_DECIMAL_LOWER_ALPHA:
			table = tableDecimalLowerAlpha;
			break;
		default:
			return null;
		}
		int seed = table.length;

		try {
			// 生成MD5串
			MessageDigest md5 = MessageDigest.getInstance("MD5");
			byte[] hash = md5.digest(str.getBytes());

			int num;			
			for (int i = 0; i < size; i ++ ) {
				num = 0xff & hash[i];
				pwd += table[num % seed];
			}

			return pwd;
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}

	/* 生成哈希key */
	public static String createHashKey(String str) {
		return createHashKey(str, 6);
	}

	public static String createHashKey(String str, int size) {
		return null;
	}
/*
	public static void main(String[] args) {
		System.out.println(virtualUUID("zhuyan@163.com"));
		return;
	}
*/
}
